require 'scraper-base.rb'
#require 'vehicle.rb'

class QatarLivingCarScraper  < ScraperBase

  attr_reader :brand
  
  def initialize(brand)
    super('qatarliving.log')
    @brand = brand
	  @site_id = 'qatarlivingcars'
		@remoteBaseURL = 'http://www.qatarliving.com'
		@startURL = "/classifieds/cars?tid=71&make=#{brand.id}"
		@detailBaseURL = 'http://www.qatarliving.com/node'		
	end
	
  def get_total_properties(page)
	  begin
      anchor = page.at("//ul[@class='pager']/li[@class='pager-last last']/a[@class='active']")
	    matches = anchor['href'].match(/page=([\d]+)/)
      @total = (matches[1].to_i + 1) * 20
    rescue
  	  @total = 'Unknown'
    end
  end
  
  def get_next_page_link(page)
    begin
      anchor = page.at("//ul[@class='pager']/li[@class='pager-next']/a[@class='active']")
      return "#{@remoteBaseURL}#{anchor['href']}"
    rescue
      nil
    end
    nil
  end

  def process_list_page(page)
    #puts page.uri.to_s
    
    new_items_found = false
    
    (page/"//table[@class='views-table cols-20']/tbody/tr").each do |tr|
      v = Vehicle.new
      v.brand = self.brand
      anchor = tr.at(".//td[@class='views-field views-field-title']/a")
      v.title = anchor.text
      pline "  Item #{@curr_property} of #{@total}..."
      # get detailed info
      href = anchor['href']
      v.id = href.match(/.*node\/([\d]+)/)[1]
      v.url = "#{@remoteBaseURL}#{href}"
      #puts "processing: #{anchor.text} [#{href}]"
      begin
        if not Vehicle.find_by_id(v.id)
          new_items_found = true
          v = process_detail_page(v)
          v.save
        end
      rescue => exception
        pline("Exception thrown while processing #{v.url}", true)
        puts exception
        puts exception.backtrace
      end
      @curr_property = @curr_property + 1
    end
    return new_items_found
  end
  
  def process_detail_page(vehicle)
    page = @agent.get vehicle.url

	  #page.save_as("detailed.html")

    datetime = node_text(page, "//span[@class='submitted']")
    m = datetime.match(/By.*\s*on\s*[\w]{3},\s*([\d\/]+)\s*-\s*(.*)/)
    vehicle.submit_date = m[1]
    vehicle.submit_time = m[2]
    
    key = nil
    gotDesc = false
    (page/"//div[@class='node cl_vehicle']/div[@class='content']/node()").each do |node|
      t = node.text
      if node.node_name == 'p'
        if gotDesc
          vehicle.others = "#{vehicle.others}\n---\n#{t}"
        else
          vehicle.description = t
          gotDesc = true
        end
      elsif node.node_name == 'div'
        arr = t.split(":");
        if arr.length >= 2
          key = arr[0].strip.downcase
          val = arr[1].gsub(/[^\d]+/, '')
          if key != nil
            case key
            when "model year"
              vehicle.model = val.to_i
            when "mileage"
              vehicle.mileage = val
            when "price"
              vehicle.price = val
            when "contact number"
              vehicle.contact_number = val
            when "vehicle type"
              vehicle.vtype = val
            end
          end
        end
      elsif node.node_name == 'fieldset'
        node.children.each do |div|
          arr = div.text.split(":")
          if arr.length >= 2
            key = arr[0].strip.downcase
            val = arr[1].gsub(/[^\d]+/, '')
            vehicle.contact_number = val if key == "contact number"
          end
        end
      end   
    end
    vehicle
  end
  
end

